//
//  DBToast.swift
//  DBCA
//
//  Created by mugua on 2024/7/2.
//

import Foundation
import MBProgressHUD

let textToastDelay = 1.5
let textFontSize = 16.0
let textColor = "FFFFFF"
let backgroundColor = "040A23"
let loadingIcon = "hud_loading"

class DBToast {
    
    
    static var instance: MBProgressHUD? = nil
    
    static func show(text: String) {
        if let keyWindow = DBKeyWindow {
            show(text: text, toView: keyWindow)
        }
    }
    
    static func show(text:String, toView: UIView) {
        show(text: text, toView: toView, hideAfterDelay: textToastDelay)
    }
    
    static func show(text: String, toView: UIView, hideAfterDelay: TimeInterval) {
        hideFrom(view: toView)
        let hud = MBProgressHUD.showAdded(to: toView, animated: true)
        hud.removeFromSuperViewOnHide = true
        hud.mode = MBProgressHUDMode.text
        hud.label.text = text
        hud.margin = 16
        hud.minSize = CGSize(width: 88, height: 44)
        hud.label.numberOfLines = 0
        hud.bezelView.style = .solidColor
        hud.bezelView.color = DBHexColor(backgroundColor)?.withAlphaComponent(0.8)
        hud.bezelView.layer.cornerRadius = 4
        hud.label.textColor = DBHexColor(textColor)
        hud.label.font = DBFuturaMaxiStd_BookFont(textFontSize)
        hud.hide(animated: true, afterDelay: hideAfterDelay)
        instance = hud
    }
    
    static func showLoading(text: String?, toView: UIView?) {
        var view = UIView()
        if let toView = toView {
            view = toView
        } else {
            if let keyWindow = DBKeyWindow {
                view = keyWindow
            }
        }
        
        if (instance != nil) && ((instance?.customView?.isKind(of: UIImageView.self)) != nil) {
            if let imageView = instance?.customView as? UIImageView {
                if imageView.animationImages?.count ?? 0 > 0 {
                    return
                }
            }
        }
        
        hideFrom(view: view)
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.removeFromSuperViewOnHide = true
        hud.mode = MBProgressHUDMode.customView
        hud.label.text = text
        hud.margin = 8
        hud.minSize = CGSize(width: 100, height: 100)
        hud.bezelView.style = .solidColor
        hud.bezelView.color = DBHexColor(backgroundColor)?.withAlphaComponent(0.8)
        hud.bezelView.layer.cornerRadius = 4
        hud.label.textColor = DBHexColor(textColor)
        hud.label.font = DBFuturaMaxiStd_BookFont(textFontSize)
        hud.label.numberOfLines = 0
        hud.isSquare = true
        
        var array = [UIImage]()
        for i in 0..<24 {
            let imageName = String(format: "\(loadingIcon)%d", i)
            if let image = UIImage(named: imageName) {
                array.append(image)
            }
        }
        
        let imageView = UIImageView()
        imageView.animationImages = array
        imageView.animationDuration = 0.5
        imageView.startAnimating()
        hud.customView = imageView
        
        instance = hud
    }
    
    static func hideFrom(view: UIView?) {
        var currView = UIView()
        if let view = view {
            currView = view
        } else {
            if let keyWindow = DBKeyWindow {
                currView = keyWindow
            }
        }
        
        MBProgressHUD.hide(for: currView, animated: true)
        instance?.hide(animated: true)
        instance = nil
    }
    
}
